<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CatesController extends Controller
{
    //设置首页
    public function getAdd()
    {
    	//查询分类数据
    	$cates = DB::table('cates')->get();
    	// dd($cates);
    	$cates = self::getCates($cates);
    	return view('cates.add',['cates'=>$cates]);
    }

    //添加数据
    public function postInsert(Request $request)
    {
    	

    	$data = $request->only(['pid','name']);
    	//判断是否为顶级路径
    	if($data['pid'] == 0){
    		$data['path'] = 0;
    	}else{
    		//拼接path
	    	$res = DB::table('cates')->where('id',$data['pid'])->first();
	    	// dd($res);
	    	$data['path'] = $res->path.','.$data['pid'];
    	}
    	//执行数据插入
    	$res = DB::table('cates')->insert($data);
  		//判断
  		if($res){
    		return redirect('/admin/cates/index')->with('success','添加成功');
    	}else{
    		return back()->with('error','数据添加失败');
    	}
    }

    //设置添加页
    public function getIndex(Request $request)
    {	
    	//递归查询数据库 进行格式化
    	// $res = self::getCatesByPid(0);
    	//调用方法查询所有数据
    	// $arr = self::getAllCates();
    	// //递归方法
    	// $res = self::getCatesByPidArr($arr,0);
    	$num = $request->input('num',10);
    	
    	if($request->input('keywords')){
    		$cates = DB::table('cates')
    		->select(DB::raw('*,concat(path,",",id) as paths'))
    		->orderBy('paths')
    		->where('name','like','%'.$request->input('keywords').'%')->paginate($num);
    	}else{
    		$cates = DB::table('cates')
    		->select(DB::raw('*,concat(path,",",id) as paths'))
    		->orderBy('paths')
    		->paginate($num);
    	}
    	//获取格式化分类数据
    	$cates = self::getCates($cates);
    	// dd($cates);
     	return view('cates.index',['cates'=>$cates,'request'=>$request->all()]);
    }

    //按照要求 格式化分类数据
    public static function getCates($cates)
    {
    	foreach($cates as $k => $v){
    		$arr = explode(',',$v->path);
    		$len = count($arr)-1;
    		// dd($cates[$k]);
    		$cates[$k]->name = str_repeat('|----',$len).$v->name;
    	}
    	//返回结果
    	return $cates;
    }

    //ajax删除用户
    public function getDelete(Request $request){
		// dd($request->all());

		$id = $request->input('id');
		$res = DB::table('cates')->where('pid',$id)->count();
		if($res > 0){
			return back()->with('error','该类下面有子类不允许删除');
		}
		$res = DB::table('cates')->where('id',$id)->delete();
		//判断
		if($res){
    		return redirect('/admin/cates/index')->with('success','删除成功');
    	}else{
    		return back()->with('error','数据删除失败');
    	}
    }

    //修改分类
    public function getEdit(Request $request){
    	$id = $request->input('id');
    	$cates = DB::table('cates')->where('id',$id)->first();
    	// dd($cates);
    	//解析模板
    	return view('cates.edit',['cates'=>$cates]);
    }

    //数据修改
    public function postUpdate(Request $request){
    	// dd($request->all());
    	$id = $request->input('id');
    	// dd($id);
    	$data = $request->except(['_token','id']);
    	// dd($data);
    	$res = DB::table('cates')->where('id',$id)->update($data);
    	
    	//判断
    	if($res){
    		return redirect('/admin/cates/index')->with('success','修改成功');
    	}else{
    		return back()->with('error','数据修改失败');
    	}

    }

    //递归查询顶级分类下的子分类 根据父级id获取父级id下所有子类 只查询到顶级分类下所有分类

    public static function getCatesByPid($pid)
    {
    	$res = DB::table('cates')->where('pid',$pid)->get();
    	$data = [];
    	foreach($res as $k => $v){
    		$v->sub = self::getCatesByPid($v->id);
    		$data[] = $v;
    	}
    	return $data;
    }

    //测试
    public function getCeshi()
    {
    	$res = self::getCatesByPid(0);
    	dd($res);
    }

    /*------------------------- 递归查询优化  -----------------------------*/

    public static function getAllCates()
    {
    	return DB::table('cates')->get();
    }

    public static function getCatesByPidArr($cates,$pid)
    {
    	$data = [];
    	foreach($cates as $k => $v){
    		if($v->pid == $pid){
    		$v->sub = self::getCatesByPidArr($cates,$v->id);
    		$data[] = $v;
    		}
    	}
    	return $data;
    }
}
